﻿using PixivVideo.Currency;
using PixivVideo.Currency.IoUtils;
using PixivVideo.Utils.Helper;
using System;
using System.IO;
using System.Windows.Forms;

namespace PixivVideo.Utils.IoUtils
{
    internal class FloderHelper
    {
        /// <summary>
        /// 初始化系统根目录
        /// </summary>
        public void InitFloder()
        {
            FloderExits(StaticValue.BinImportPath, true);
            FloderExits(StaticValue.TempPath, true);
        }

        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="FloderPath"></param>
        public void OpenFloder(string FloderPath)
        {
            if (TextHelper.JudgeNull(FloderPath))
                return;
            System.Diagnostics.Process.Start(FloderPath);
        }

        /// <summary>
        /// 删除这个文件夹里所有子文件
        /// </summary>
        /// <param name="FloderPath"></param>
        public void DeleteAllChildrenFile(String FloderPath)
        {
            foreach (string line in new FloderNameHelper().GetFullFileNameList(FloderPath))
            {
               new  FileHelper().FileDelete(line);
            }
        }

        /// <summary>
        /// 判断文件夹是否存在
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="CreatFlag"></param>
        /// <returns></returns>
        public bool FloderExits(String FloderPath, bool CreatFlag)
        {
            if (!Directory.Exists(FloderPath))
            {
                if (!CreatFlag)
                {
                    return false;
                }

                Console.WriteLine("已创建" + FloderPath);
                try { Directory.CreateDirectory(FloderPath); }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                    PrintLog.Log("异常文件夹名字" + FloderPath);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 删除文件夹  包括里面的文件
        /// </summary>
        /// <param name="FloderPath"></param>
        public void FolderDelete(String FloderPath)
        {
            try
            {
                Directory.Delete(FloderPath, true);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 创建一个文件夹
        /// </summary>
        /// <param name="FloderPath"></param>
        public void FolderCreate(String FloderPath)
        {
            try
            {
                Directory.CreateDirectory(FloderPath);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 移动文件夹文件到新文件夹
        /// </summary>
        /// <param name="OldFloder"></param>
        /// <param name="NewFloder"></param>
        public void FloderFileMove(string OldFloder, string NewFloder)
        {
            if (!FloderExits(NewFloder, true))
            {
                Console.WriteLine("创建新文件夹失败");
                return;
            }
            foreach (string line in new FloderNameHelper().GetFullFileNameList(OldFloder))
            {
                new FileHelper().FileMove(line, Path.GetFullPath(NewFloder) + "\\" + Path.GetFileName(line), true);
            }
        }

        /// <summary>
        /// 移动文件到子文件夹
        /// </summary>
        /// <param name="FilePath">原始文件路径。例如：c:\\cat\232.txt</param>
        /// <param name="ChildFloderName">例如 ：child</param>
        public void MoveToChildFloder(string FilePath, string ChildFloderName)
        {
            string FloderPath = new FloderNameHelper().GetFloderPath(FilePath) + ChildFloderName + "\\";
            FloderExits(FloderPath, true);
            string NewFileName = FloderPath + new FileNameHelper().GetFileName(FilePath);
            try
            {
                // 确认目标文件不存在
                if (File.Exists(FloderPath))
                {
                    File.Delete(FloderPath);
                }
                File.Move(FilePath, NewFileName);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 选择一个文件夹
        /// </summary>
        /// <returns></returns>
        public string SelectFloder()
        {
            using (FolderBrowserDialog SelectFileFolder = new FolderBrowserDialog())
            {
                if (SelectFileFolder.ShowDialog() == DialogResult.OK)
                {
                    return SelectFileFolder.SelectedPath + "\\";
                }
                else
                    return "";
            }
        }
    }
}